package com.systop.party05.base;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.systop.party05.R;
import com.systop.party05.http.CommonOkhttp;
import com.systop.party05.utils.ScreenManagerUtils;
import com.systop.party05.utils.ToastUtils;

public class BaseActivity extends AppCompatActivity {
    private ImageButton baseIbtnBack; //按钮控件
    private TextView baseTvTitle;//文本控件
    private FrameLayout baseFlContent;//帧布局控件
    private RelativeLayout baseRl;//相对布局控件

    public CommonOkhttp commonOkhttp;
    public Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        ScreenManagerUtils.getInstance().addActivity(this);
        initView();
        gson = new Gson();
        commonOkhttp = new CommonOkhttp();
    }

    private void initView(){//绑定控件与对象
        baseIbtnBack=findViewById(R.id.base_ibtn_back);
        baseTvTitle=findViewById(R.id.base_tv_title);
        baseFlContent=findViewById(R.id.base_fl_content);
        baseRl=findViewById(R.id.base_rl);
    }

    /**
     * 将所有继承BaseActivity的activity设置到BaseActivity的FrameLayout中的方法
     * @param layoutID
     */
    public void setBaseContentView(int layoutID){
        View view= LayoutInflater.from(this).inflate(layoutID,null);//实例化布局文件
        //设置布局对象宽度、高度等属性
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);//设置该布局对象到视图中
        baseFlContent.addView(view);
    }

    /**
     * 标题栏标题
     * @param title 标题文本
     */
    public void setTvTitle(String title){
        baseRl.setVisibility(View.VISIBLE);
        baseTvTitle.setVisibility(View.VISIBLE);
        baseTvTitle.setText(title!=null?title:"");
    }

    /**
     * 标题栏返回按钮
     */
    public void setIvBack(){
        baseRl.setVisibility(View.VISIBLE);//设置控件可见
        baseIbtnBack.setVisibility(View.VISIBLE);
        baseIbtnBack.setOnClickListener(new View.OnClickListener() {//设置点击监听器
            @Override
            public void onClick(View v) {
                finishActivity();//结束Activity,返回上一个Activity
            }
        });

    }

    /**
     * 物理返回键
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode,event);

    }

    /**
     * 关闭当前activity
     */
    public void finishActivity(){
           ScreenManagerUtils.getInstance().removeActivity(BaseActivity.this);
    }

    //弹框界面
    public void showMessage(String message){
        ToastUtils.getInstance(BaseActivity.this).showMessage(message);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        ToastUtils.getInstance(BaseActivity.this).toastCancel();

    }

}